package com.feeyo.groundservice.composewidget.view.title_bar

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import com.blankj.utilcode.util.BarUtils
import com.feeyo.groundservice.composewidget.R
import com.feeyo.groundservice.composewidget.util.dimenToDp
import com.feeyo.groundservice.composewidget.util.dimenToSp
import com.feeyo.groundservice.composewidget.util.dpToPx
import com.feeyo.groundservice.composewidget.util.intervalClick
import com.feeyo.groundservice.composewidget.util.pxToDp
import com.feeyo.groundservice.composewidget.util.toComposeColor

/**
 *作者：QLH on 2023-07-28
 *描述：
 */

/**公共标题栏,最底层的，一般不使用**/
@Composable
fun ColumnScope.CommonTitleView(
    title: String,
    titleColor: Color = R.color.white.toComposeColor(),
    titleSize: TextUnit = R.dimen.d16.dimenToSp(),
    bg: Color = R.color.blue_2E76EE.toComposeColor(),
    height: Dp = R.dimen.d44.dimenToDp(),
    iconColor: Color = R.color.white.toComposeColor(),
    arrowBack: Painter? = null,
    topPadding: Int = 0,
    onBack: () -> Unit = {},
    titleContent: @Composable (BoxScope.() -> Unit)? = null,
    rightContent: @Composable RowScope.() -> Unit = {}
) {
    val statusBarHeightDp = topPadding.pxToDp()
    val density = LocalDensity.current
    Log.e(
        "CommonTitleView",
        "density:${density.density}---originalHeightPx:$topPadding---originalHeightDp:$statusBarHeightDp---realPx:${statusBarHeightDp.dpToPx()}"
    )
    Box(
        modifier = Modifier
            .padding(top = statusBarHeightDp)
            .height(height)
            .fillMaxWidth()
            .background(bg)

    ) {
        Image(
            painter = arrowBack ?: painterResource(R.drawable.icon_back),
            contentDescription = null,
            modifier = Modifier.size(height - R.dimen.d4.dimenToDp())
                .align(Alignment.CenterStart)
                .intervalClick {
                    onBack()
                },
            contentScale = ContentScale.Inside,
            colorFilter = ColorFilter.tint(iconColor)
        )

        Box(
            modifier = Modifier.align(Alignment.Center),
            contentAlignment = Alignment.Center,
        ) {
            titleContent?.invoke(this) ?: Text(
                text = title,
                color = titleColor,
                fontSize = titleSize,
                fontWeight = FontWeight.W500,
            )
        }

        Row(
            modifier = Modifier.align(Alignment.CenterEnd)
                .fillMaxHeight()
                .padding(end = R.dimen.d10.dimenToDp()),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            rightContent()
        }
    }
}